package com.bhkj.pdjhbgnewproject.core.main;

import android.app.Activity;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.constraint.ConstraintLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.bhkj.pdjhbgnewproject.R;
import com.bhkj.pdjhbgnewproject.common.bean.WindowBean;
import com.bhkj.pdjhbgnewproject.common.contanct.Contacts;
import com.bhkj.pdjhbgnewproject.common.utils.NetIpUtils;
import com.bhkj.pdjhbgnewproject.common.utils.Tools;
import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ObjectUtils;
import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.CustomTarget;
import com.bumptech.glide.request.transition.Transition;
import com.zhouyou.http.EasyHttp;
import com.zhouyou.http.callback.SimpleCallBack;
import com.zhouyou.http.exception.ApiException;

import java.net.InetAddress;

public class MainViewModel {
    private Resources resources;
    private TextView deptNameTv, windowNumTv;
    private ImageView iconIv;
    private ConstraintLayout rootLayout;

    public MainViewModel(Resources resources, ImageView iconIv, ConstraintLayout rootLayout, TextView... tvs) {
        this.resources = resources;
        this.deptNameTv = tvs[0];
        this.windowNumTv = tvs[1];
        this.iconIv = iconIv;
        this.rootLayout = rootLayout;
    }

    /**
     * 获取屏幕展示信息
     */
    public void obtainWindowShowInfo() {
        InetAddress localIPAddress = NetIpUtils.getLocalIPAddress();
        if (localIPAddress == null) {
            return;
        }
        EasyHttp.get("windows/findWindowsInfoByIp")
                .params("deptid", Tools.encrypt(resources.getString(R.string.deptId)))
                .params("wip", localIPAddress.getHostAddress())
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onError(ApiException e) {
                        ToastUtils.showShort("访问服务器失败:" + e.getMessage());
//                        LogUtils.e(e.getCode() + "-" + e.getMessage());
                    }

                    @Override
                    public void onSuccess(String s) {
                        LogUtils.e(s + "测试");
                        WindowBean windowBean = GsonUtils.fromJson(s, WindowBean.class);
                        if (windowBean == null) {
                            return;
                        }
                        if (windowBean.isSuccess()) {
                            WindowBean.Datas datas = windowBean.getDatas();
                            if (ObjectUtils.isNotEmpty(datas)) {
                                if (deptNameTv != null) {
                                    deptNameTv.setText(datas.getDeptname());
                                }
                                LogUtils.d("部门名称:" + datas.getDeptname());
                                String windowNum = datas.getName();
                                if (windowNumTv != null && ObjectUtils.isNotEmpty(windowNum)) {
                                    windowNumTv.setText(windowNum.substring(0, windowNum.length() - 1));
                                }
//                                SPUtils.getInstance().put(Contacts.PARAM_WINDOW, datas.getName());
//                                SPUtils.getInstance().put(Contacts.PARAM_DEPT, datas.getDeptname());
                                setIconPic(datas.getLogo());
                                String windowStatus = SPUtils.getInstance().getString(Contacts.WINDOW_STATUS);
                                if (ObjectUtils.equals(windowStatus, Contacts.PUSH_LOGIN) ||
                                        ObjectUtils.equals(windowStatus, Contacts.PUSH_RESUME_SERVICE)) {
                                    rootLayout.setBackgroundResource(R.drawable.bg);
                                } else {
                                    setRootBackground(datas.getUrl());

                                }
                            } else {
                                ToastUtils.showShort("返回的窗口信息为空");
                            }

                        } else {
                            ToastUtils.showShort(windowBean.getMsg());
                        }
                    }
                });

    }


    private void setIconPic(String url) {
        Activity topActivity = ActivityUtils.getTopActivity();
        if (iconIv != null && topActivity != null) {
            RequestOptions requestOptions = new RequestOptions().fitCenter().override(100, 100);
            Glide.with(topActivity).applyDefaultRequestOptions(requestOptions).load(url)
                    .into(iconIv);
        }
    }

    private void setRootBackground(String url) {
        Activity topActivity = ActivityUtils.getTopActivity();
        if (rootLayout != null && topActivity != null) {
            RequestOptions requestOptions = new RequestOptions().fitCenter().override(1920, 1080);
            Glide.with(topActivity).applyDefaultRequestOptions(requestOptions).asBitmap()
                    .load(url).into(new CustomTarget<Bitmap>() {
                @Override
                public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
                    rootLayout.setBackground(new BitmapDrawable(resources, resource));
                }

                @Override
                public void onLoadCleared(@Nullable Drawable placeholder) {

                }
            });
        }
    }
}
